package Client;

import Message.AccountLoginInfo;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;

import javax.swing.*;

public class LoginActionListener implements ActionListener {

    User user;
    LoginUI loginui;
    private Point point;
    private boolean isChecked;
    private AccountLoginInfo accountLoginInfo;

    LoginActionListener(LoginUI l) {//创建监听器对象的同时设置监听器的服务对象
        this.loginui = l;

        this.loginui.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                point = e.getPoint();
            }
        });
        this.loginui.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                Point newpoint = e.getLocationOnScreen();
                loginui.setLocation(newpoint.x - point.x, newpoint.y - point.y);
            }
        });
        new Thread() {// 创建新线程
            public void run() {
                int screenwidth, screenheight;
                Toolkit kit = Toolkit.getDefaultToolkit();
                Dimension screensize = kit.getScreenSize();
                screenwidth = (int) screensize.getWidth();
                screenheight = (int) screensize.getHeight();
                for (int i = 0; i <= 340; i += 85) {// 循环拉伸窗体
                    loginui.setBounds(screenwidth / 2 - 437 / 2, screenheight / 2 - i / 2, 437, i);// 不断设置窗体大小与位置
                    try {
                        Thread.sleep(5);// 线程休眠10毫秒
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }.start();// 启动线程

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == loginui.bMin) {
            loginui.setExtendedState(JFrame.ICONIFIED);
        }

        if (e.getSource() == loginui.bExit) {
            System.exit(0);
        }

        if (e.getSource() == loginui.bLogin) {
            String uId, uPass;
            uId = LoginUI.UidText.getText().trim();
            uPass = String.valueOf(LoginUI.UpswText.getPassword());

            System.out.println("uid："+uId);
            System.out.println("uPass："+uPass);

            if (uId.equals("")) {
                JOptionPane.showMessageDialog(null, "请输入账号！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (uPass.equals("")) {
                JOptionPane.showMessageDialog(null, "请输入密码！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

			if (uPass.length() < 4 || 20 < uPass.length()) {
				JOptionPane.showMessageDialog(null, "密码错误！", "消息提示", JOptionPane.WARNING_MESSAGE);
				return;
			}
			User user=new User(uId,uPass);
			accountLoginInfo=new AccountLoginInfo(user);
            try {
                Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_LOGIN_PORT);
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(accountLoginInfo);
                objectOutputStream.flush();
                //获得服务器的回应。
                InputStream inputStream = socket.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String res = null;
                res = bufferedReader.readLine();

                if (res.equals("true")) {
                    this.user=new User(uId,uPass,1);
                    new MainUI(this.user);
                    loginui.dispose();
                }else {
                    JOptionPane.showMessageDialog(null, "登录失败，请检查账号或密码！", "消息提示", JOptionPane.WARNING_MESSAGE);
                }

            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "处理用户登录的服务器不在线！", "消息提示", JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        } else if (e.getSource() == loginui.bCancel) {
            System.exit(0);
        } else if (e.getSource() == loginui.bSignup) {
            new SignUpUI();
        }
    }


}
